//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "IborCouponPricer.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletVolatilityStructure.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/FloatingRateCouponPricer.h>
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CIborCouponPricer::CIborCouponPricer (boost::shared_ptr<QuantLib::IborCouponPricer>& childNative, Object^ owner) : CFloatingRateCouponPricer(CIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phIborCouponPricer = NULL;
#endif
	_ppIborCouponPricer = &childNative;
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppIborCouponPricer));
}
Cephei::QL::Cashflows::CIborCouponPricer::CIborCouponPricer (QuantLib::IborCouponPricer& childNative, Object^ owner) : CFloatingRateCouponPricer(CIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phIborCouponPricer = NULL;
#endif
	_ppIborCouponPricer = new boost::shared_ptr<QuantLib::IborCouponPricer> (&childNative);
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppIborCouponPricer));
    _IborCouponPricerOwner = owner;
    _FloatingRateCouponPricerOwner = owner;
}

Cephei::QL::Cashflows::CIborCouponPricer::CIborCouponPricer (CIborCouponPricer^ copy) : CFloatingRateCouponPricer(CIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phIborCouponPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIborCouponPricer = new boost::shared_ptr<QuantLib::IborCouponPricer> (copy->GetShared());
        _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppIborCouponPricer));
    }
}
Cephei::QL::Cashflows::CIborCouponPricer::CIborCouponPricer (PLATFORM::Type^ t) : CFloatingRateCouponPricer(CIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phIborCouponPricer = NULL;
#endif
	if (!t->IsSubclassOf(CIborCouponPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CIborCouponPricer::CIborCouponPricer (QuantLib::Handle<QuantLib::IborCouponPricer>& childNative, Object^ owner)  : CFloatingRateCouponPricer(CIborCouponPricer::typeid)
{
	_phIborCouponPricer = &childNative;
	_ppIborCouponPricer = &static_cast<boost::shared_ptr<QuantLib::IborCouponPricer>>(childNative.currentLink());
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppIborCouponPricer));
    _IborCouponPricerOwner = owner;
}
Cephei::QL::Cashflows::CIborCouponPricer::CIborCouponPricer (QuantLib::Handle<QuantLib::IborCouponPricer> childNative)  : CFloatingRateCouponPricer(CIborCouponPricer::typeid)
{
	_phIborCouponPricer = &childNative;
	_ppIborCouponPricer = &static_cast<boost::shared_ptr<QuantLib::IborCouponPricer>>(childNative.currentLink());
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppIborCouponPricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CIborCouponPricer::CIborCouponPricer (QuantLib::IborCouponPricer childNative)  : CFloatingRateCouponPricer(CIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phIborCouponPricer = NULL;
#endif
	_ppIborCouponPricer = new boost::shared_ptr<QuantLib::IborCouponPricer> (new QuantLib::IborCouponPricer (childNative));
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppIborCouponPricer));
}
#endif

Cephei::QL::Cashflows::CIborCouponPricer::~CIborCouponPricer ()
{
    if (_ppIborCouponPricer != NULL)
    {
	    delete _ppIborCouponPricer;
        _ppIborCouponPricer = NULL;
    }
}
Cephei::QL::Cashflows::CIborCouponPricer::!CIborCouponPricer ()
{
    if (_ppIborCouponPricer != NULL)
    {
	    delete _ppIborCouponPricer;
    }
}
QuantLib::IborCouponPricer& Cephei::QL::Cashflows::CIborCouponPricer::GetReference ()
{
    if (_ppIborCouponPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppIborCouponPricer;
}
boost::shared_ptr<QuantLib::IborCouponPricer>& Cephei::QL::Cashflows::CIborCouponPricer::GetShared ()
{
    if (_ppIborCouponPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppIborCouponPricer;
}
QuantLib::IborCouponPricer* Cephei::QL::Cashflows::CIborCouponPricer::GetPointer ()
{
    if (_ppIborCouponPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppIborCouponPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::IborCouponPricer>& Cephei::QL::Cashflows::CIborCouponPricer::GetHandle ()
{
	if (_phIborCouponPricer == NULL)
	{
		_phIborCouponPricer = new Handle<QuantLib::IborCouponPricer> (*_ppIborCouponPricer);
	}
	return *_phIborCouponPricer;
}
#endif
bool Cephei::QL::Cashflows::CIborCouponPricer::HasNative () 
{
	return (_ppIborCouponPricer != NULL);
}

Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^ Cephei::QL::Cashflows::CIborCouponPricer::CapletVolatility::get ()
{
    try
    {
    	Handle<QuantLib::OptionletVolatilityStructure> _rv = (Handle<QuantLib::OptionletVolatilityStructure>)(*_ppIborCouponPricer)->capletVolatility ( );   
        Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure^ _nrv = REFNEW Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IIborCouponPricer^ Cephei::QL::Cashflows::CIborCouponPricer::SetCapletVolatility (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^>^ v)
{
    COptionletVolatilityStructure^ _Cv;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^>::IsSome::get (v))
        {
            _Cv = safe_cast<COptionletVolatilityStructure^> (v->Value);
            _Cv->Lock();
        }
        Handle<QuantLib::OptionletVolatilityStructure>& _v = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^>::IsSome::get (v) ? static_cast<Handle<QuantLib::OptionletVolatilityStructure>&> (_Cv->GetHandle ()) : Handle<QuantLib::OptionletVolatilityStructure>()); //6
    	(*_ppIborCouponPricer)->setCapletVolatility ( _v );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cv != nullptr) _Cv->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

